from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from try_mysql import *
from PyQt5.uic import loadUiType
import begin_py
import sys
ui, _ = loadUiType('Manege_Password.ui')

class Password_App(QMainWindow, ui):
    #定义构造
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.handle_buttons()
        self.new_password.setEchoMode(QLineEdit.Password)
        self.confirm_new_password.setEchoMode(QLineEdit.Password)
        self.show()

    def handle_buttons(self):
        self.confirm_manage.clicked.connect(self.PassWord)
        self.cancel_manage.clicked.connect(self.Close)

    def PassWord(self):
        conn = get_conn()
        cur = conn.cursor()
        StaffNum= self.set_staff_num.text()
        StaffPassword = self.new_password.text()
        Staffpassword = self.confirm_new_password.text()
        if(StaffNum=="" or StaffPassword=="" or Staffpassword==""):
            print(QMessageBox.information(self, "提醒", "员工号码与密码不得为空", QMessageBox.Yes, QMessageBox.Yes))
            return
        if(StaffPassword!=Staffpassword):
            print(QMessageBox.information(self,"提醒","前后两次密码不一致",QMessageBox.Yes, QMessageBox.Yes))
            return
        else:
            sql1 = "update user set StaffPassword=%s where StaffNum=%s"
            cur.execute(sql1, (StaffPassword, StaffNum))
            conn.commit()
            close_conn(conn, cur)
            print(QMessageBox.information(self, "提醒", "密码更改成功", QMessageBox.Yes, QMessageBox.Yes))
            self.Back = begin_py.Begin_App()
            self.close()
            self.Back.show()
        return

    def Close(self):
        self.Back = begin_py.Begin_App()
        self.close()
        self.Back.show()




def main():
    app = QApplication([])
    window = Password_App()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()
